package com.ruijiang.service.VmService.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ruijiang.dto.insert.NodeInsertDto;
import com.ruijiang.dto.page.RegionPageDto;
import com.ruijiang.entity.Node;
import com.ruijiang.mapper.VmServiceMapper.BusinessTypeMapper;
import com.ruijiang.mapper.VmServiceMapper.NodeMapper;
import com.ruijiang.mapper.VmServiceMapper.RegionMapper;
import com.ruijiang.result.PageResult;
import com.ruijiang.service.VmService.NodeService;
import com.ruijiang.vo.businessType.BusinessBaseVO;
import com.ruijiang.vo.node.NodeEntryVO;
import com.ruijiang.vo.node.NodeInfoVO;
import com.ruijiang.vo.node.NodePageVO;
import com.ruijiang.vo.region.RegionInfoVO;
import com.ruijiang.vo.vm.VMInfoVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class NodeServiceImpl implements NodeService {

    @Autowired
    NodeMapper nodeMapper;
    @Autowired
    RegionMapper regionMapper;
    @Autowired
    BusinessTypeMapper businessTypeMapper;

    @Override
    public PageResult getNodeList(RegionPageDto dto) {
        PageHelper.startPage(dto.getPageIndex(),dto.getPageSize());
        Page page = (Page) nodeMapper.getNodeList(dto);
        return PageResult.pageBuild(page);

    }

    /**
     * 新增点位
     * @param dto
     */
    @Override
    public void insertNode(NodeInsertDto dto) {
        Node node = new Node();
        BeanUtils.copyProperties(dto,node);
        node.setCreateTime(LocalDateTime.now());
        node.setUpdateTime(LocalDateTime.now());
        nodeMapper.insertNode(node);
    }

    /**
     * 修改点位
     * @param id
     * @param dto
     */
    @Override
    public void updateNodeById(String id, NodeInsertDto dto) {
        nodeMapper.updateNodeById(id,dto);
    }

    /**
     * 删除点位
     * @param id
     */
    @Override
    public void deleteNodeById(String id) {
        nodeMapper.deleteNodeById(id);

    }

    /**
     * 获取点位详情
     * @param id
     * @return
     */
    @Override
    public List<VMInfoVO> getVmListByNodeId(String id) {
        return nodeMapper.getVmListByNodeId(id);
    }

    /**
     * 获取点位信息
     * @param id
     * @return
     */
    public Node getNodeByNodeId(String id){
        return nodeMapper.getNodeByNodeId(id);
    }


    /**
     * 获取点位设备数量
     * @param id
     * @return
     */
    public String getNodeVMCountByNodeId(String id){
        return nodeMapper.getNodeVMCountByNodeId(id);
    }

    //根据nodeid查询nodeinfovo
    public NodeInfoVO getRegBusByNodeId(String nodeId){
        //查询点位信息
        NodeInfoVO nodeInfoVO =new NodeInfoVO();
        Node node = getNodeByNodeId(nodeId);
        BeanUtils.copyProperties(node,nodeInfoVO);
        //获取点位设备数量
        nodeInfoVO.setVmCount(getNodeVMCountByNodeId(nodeId));
        String regionId = node.getRegionId();
        //获取点位区域信息
        RegionInfoVO regionById = regionMapper.getRegionById(regionId);
        nodeInfoVO.setRegion(regionById);
        //获取商圈信息
        Integer businessId = node.getBusinessId();
        BusinessBaseVO businessBaseVO = businessTypeMapper.getBusinessTypeById(businessId);
        nodeInfoVO.setBusinessType(businessBaseVO);

        return nodeInfoVO;

    }

    /**
     * 获取合作商点位汇总统计
     * @return
     */
    @Override
    public List<NodeEntryVO> getNodeCollect() {

        return nodeMapper.getNodeCollect();

    }

    @Override
    public Integer getNodeCount() {
        return nodeMapper.getNodeCount();
    }
}
